package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IContentService;
import com.mall.shopping.IHomeService;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: lds
 * @clsss: ShoppingHomepageController
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
@Api(tags = "ShoppingHomepageController", description = "商城首页控制层")
public class ShoppingHomepageController {
    @Reference(interfaceClass = IContentService.class, timeout = 3000, retries = 0, check = false)
     IContentService contentService;

    @Reference(interfaceClass = IProductCateService.class, timeout = 10000, retries = 0, check = false)
     IProductCateService productCateService;

    @Reference(interfaceClass = IProductService.class, timeout = 10000, retries = 0, check = false)
     IProductService productService;

    @Reference(interfaceClass = IHomeService.class, timeout = 3000, retries = 0, check = false)
    IHomeService iHomeService;




        @Anoymous
        @RequestMapping("homepage")
        public ResponseData shoppingHomepage () {
            HomePageResquest homePageResquest = new HomePageResquest();

            HomePageResponse homePageResponse = iHomeService.homepage(homePageResquest);
            if (ShoppingRetCode.SHOPPING_HOMEPAGE.getCode().equals(homePageResponse.getCode())) {
                //本次调用成功
                return new ResponseUtil().setData(homePageResponse.getPanelContentItemDtos());
            }
            //请求失败
            return new ResponseUtil().setErrorMsg(homePageResponse.getMsg());
        }

        @Anoymous
        @GetMapping("navigation")
        public ResponseData shoppingNavigation () {
            NavListResponse navListResponse = contentService.queryNavList();

            if (ShoppingRetCode.SHOPPING_HOMEPAGE.getCode().equals(navListResponse.getCode())) {
                return new ResponseUtil().setData(navListResponse.getPannelContentDtos());
            }
            return new ResponseUtil().setErrorMsg(navListResponse.getMsg());
        }

        @Anoymous
        @GetMapping("categories")
        public ResponseData shoppingCategories (String sort){
            AllProductCateRequest allProductCateRequest = new AllProductCateRequest();
            allProductCateRequest.setSort(sort);
            AllProductCateResponse allProductCate = productCateService.getAllProductCate(allProductCateRequest);
            if (ShoppingRetCode.SHOPPING_HOMEPAGE.getCode().equals(allProductCate.getCode())) {

                return new ResponseUtil().setData(allProductCate.getProductCateDtoList());
            }

            return new ResponseUtil().setErrorMsg(allProductCate.getMsg());

        }

        @Anoymous
        @GetMapping("product/{id}")
        public ResponseData shoppingGetId (@PathVariable("id") Long id){
            ProductDetailRequest productDetailRequest = new ProductDetailRequest();
            productDetailRequest.setId(id);
            ProductDetailResponse productDetail = productService.getProductDetail(productDetailRequest);
            if (ShoppingRetCode.SUCCESS.getCode().equals(productDetail.getCode())) {
                return new ResponseUtil().setData(productDetail.getProductDetailDto());
            }
            return new ResponseUtil().setErrorMsg(productDetail.getMsg());
        }


}
